package com.android.fragmentdemo
import android.os.Bundle
import android.util.Log
import androidx.recyclerview.widget.LinearLayoutManager
import com.android.fragmentdemo.adapters.DynamicItem03Adapter
import com.hengfoxwolf.commonbase.base.BaseActivity3
import com.android.fragmentdemo.databeans.DataFactoryIp
import com.android.fragmentdemo.databinding.ActivityPullToRefereshBinding
import com.android.fragmentdemo.defineviews.DeOverScrollRecyclerView
import com.android.fragmentdemo.defineviews.List01DataItemDecoration
import com.android.fragmentdemo.defineviews.PullToRefreshLayout
import com.android.fragmentdemo.util.StepLoadCollectionUtil
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class PullToRefreshActivity :
    BaseActivity3<ActivityPullToRefereshBinding>(ActivityPullToRefereshBinding::inflate),PullToRefreshLayout.IPullToRefreshCallback {

    private lateinit var mDyAdapter: DynamicItem03Adapter

    override fun init(savedInstanceState: Bundle?) {
        mDyAdapter = DynamicItem03Adapter()
        val dyLayoutManager = LinearLayoutManager(this@PullToRefreshActivity)
        StepLoadCollectionUtil.init(DataFactoryIp.createStrDataSet(), 15)
        mDyAdapter.itemDataSet = StepLoadCollectionUtil.getInitDataSet()
        mBinding.rvToRefresh.apply {
            layoutManager = dyLayoutManager
            adapter = mDyAdapter
            addItemDecoration(
                List01DataItemDecoration(
                    this@PullToRefreshActivity,
                    0F
                ).apply {
                    setDrawLastLine(false)
                })
            initAdapter(mDyAdapter)
            setActionState(DeOverScrollRecyclerView.ActionState.PullToRefresh)
        }
        mBinding.pullToRefreshLayout.setPullToRefreshCallback(this)
    }

    override fun pullDownToRefresh() {
        CoroutineScope(Dispatchers.Main).launch {
            delay(2000)
            Log.d(TAG, "override fun pullDownToRefresh()")
            //mDyAdapter.setItemDataSet(DataFactoryIp.createStrDataSet().subList(0, 10), false)
            mBinding.pullToRefreshLayout.resetViewState()
        }
    }

    override fun pullUpToLoad() {}

    companion object {
        const val TAG = "PullToRefreshActivity"
    }
}